package _15_generics._15_3;

import java.util.Iterator;

/**
 * 使用适配器创建迭代器
 */
public class IterableFibonacci extends Fibonacci implements Iterable<Integer>{
    private int n;
    public IterableFibonacci(int n){
        this.n = n;
    }
    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            @Override
            public boolean hasNext() {
                return n > 0;
            }

            @Override
            public Integer next() {
                n -- ;
                return IterableFibonacci.this.next();
            }
        };
    }

    public static void main(String[] args) {
        for (int n : new IterableFibonacci(18)){
            System.out.print(n + " ");
        }
    }
}
